package com.lzx.adx.report.support.strategy.v2.channel;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.XIN_YI)
public class XinYiReportStrategy extends AbstractReport {
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String,Object> params=new HashMap<>();
        params.put("ad_id",reportDTO.getContentId());
        params.put("campaign_id",reportDTO.getCampaignId());
        params.put("idfa",reportDTO.getIdfa());
        params.put("idfa_md5",reportDTO.getIdfaSum());
        params.put("imei",reportDTO.getImei());
        params.put("imei_md5",reportDTO.getImeiSum());
        params.put("oaid",reportDTO.getOaid());
        params.put("oaid_md5",reportDTO.getOaidSum());
        params.put("os_type",reportDTO.getOs());
        params.put("ip",reportDTO.getIp());
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        params.put("request_id",reportDTO.getBizRequestId());

        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback_url=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }

}
